﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using elr_tracking_dal;
using elr_tracking_bal.ART;

namespace elr_tracking_bal
{
    public class ClaseItems
    {
        public List<ListaITEM> ListaRecibida { get; set; }
        public int ID_Lote;
        public int ID_POM;
        public int ID_Proveedor;
        public string factura;
        public float importe_f;

        public ClaseItems(List<ListaITEM> ListaRecibida, int ID_Lote, int ID_POM, int Proveedor)
        {
            this.ListaRecibida = ListaRecibida;
            this.ID_Lote = ID_Lote;
            this.ID_POM = ID_POM;
            this.ID_Proveedor = Proveedor;
        }

        public ClaseItems(int ID_Lote, int ID_POM, int Proveedor)
        {
            this.ID_POM = ID_POM;
            this.ID_Lote = ID_Lote;
            this.ID_Proveedor = Proveedor;
        }

        public ClaseItems()
        {

        }

        public void IngresarNuevoLote(string POMserial, string LoteName, string Pedimento) {

            string query = "";
            query += "EXEC SP_INSERT_NEW_LOTE ";

	        query += " @POM_SERIAL = '"+POMserial+"'";
	        query += " ,@LOTE_NAME = '"+LoteName+"'";
            query += " ,@PEDIMENTO = '" + Pedimento + "'";

            DataTable dt = new DataTable();
            dt = SrvDB.ExecuteQuery(query);

            this.ID_Lote = Int32.Parse(dt.Rows[0][0].ToString());
            this.ID_POM = Int32.Parse(dt.Rows[0][1].ToString());
        }

        public List<VisibleArticulo> LlenarGrid1()
        {
            List<VisibleArticulo> ListaRes = new List<VisibleArticulo>();
            DataTable dt = new DataTable();

            dt = SrvDB.ExecuteQuery("EXEC SP_FILL_GRID1 @ID_LOTE = " + ID_Lote + " ,@ID_POM = " + ID_POM + " ,@ID_PROVEEDOR = " + ID_Proveedor);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Articulo aux = new Articulo();
                aux.EQUIPO = new Equipo();
                aux.MARCA = new Marca();
                aux.CATEGORIA = new Categoria();
                aux.MODELO = new Modelo();
                aux.MONEDA = new Moneda();

                aux.ID_ARTICULO = Int32.Parse(dt.Rows[i][0].ToString());
                aux.ARTICULO = dt.Rows[i][1].ToString();
                aux.EQUIPO.ID_EQUIPO = Int32.Parse(dt.Rows[i][3].ToString());
                aux.EQUIPO.EQUIPO = dt.Rows[i][6].ToString();
                aux.MARCA.MARCA = dt.Rows[i][8].ToString();
                aux.MARCA.ID_MARCA = Int32.Parse(dt.Rows[i][4].ToString());
                aux.CATEGORIA.CATEGORIA = dt.Rows[i][7].ToString();
                aux.CATEGORIA.ID_CATEGORIA = Int32.Parse(dt.Rows[i][2].ToString());
                aux.MODELO.MODELO = dt.Rows[i][9].ToString();
                aux.MODELO.ID_MODELO = Int32.Parse(dt.Rows[i][5].ToString());
                aux.IDENTIFICADOR = dt.Rows[i][10].ToString();
                aux.CANTIDAD = (float)Convert.ToDouble(dt.Rows[i][11].ToString());
                
                aux.COSTOU = (float)Convert.ToDouble(dt.Rows[i][12].ToString());
                aux.MONEDA.ID_MONEDA = Int32.Parse(dt.Rows[i][13].ToString());
                aux.MONEDA.MONEDA = dt.Rows[i][14].ToString();
                aux.IVA = (float)Convert.ToDouble(dt.Rows[i][15].ToString());          		
   
                VisibleArticulo AuxVis = new VisibleArticulo(aux.ID_ARTICULO, aux.ARTICULO, aux.CATEGORIA, aux.EQUIPO, aux.MARCA, aux.MODELO, aux.IDENTIFICADOR, aux.CANTIDAD, aux.COSTOU, aux.MONEDA.ID_MONEDA, aux.MONEDA.MONEDA, aux.IVA);

                ListaRes.Add(AuxVis);
            }

            return ListaRes;
        }

        public List<ListaITEM> LlenarGrid2()
        {
            List<ListaITEM> ListaRes = new List<ListaITEM>();
            DataTable dt = new DataTable();

            dt = SrvDB.ExecuteQuery("EXEC SP_FILL_GRID2 @ID_LOTE = " + ID_Lote + " ,@ID_POM = " + ID_POM + ", @Prov = " + ID_Proveedor);

            for (int i = 0; i < dt.Rows.Count; i++) {

                ListaITEM aux = new ListaITEM();

                aux.Nombre_Articulo = dt.Rows[i][0].ToString();
                aux.Serie = dt.Rows[i][1].ToString();
                aux.Sku = dt.Rows[i][2].ToString();

                if(dt.Rows[i][3].ToString().Equals("True"))
                    aux.Is_Arrendado = true;
                else
                    aux.Is_Arrendado = false;

                aux.Costo_Final = Int32.Parse(dt.Rows[i][4].ToString());
                aux.ID_Item = Int32.Parse(dt.Rows[i][5].ToString());
                aux.ID_Track = Int32.Parse(dt.Rows[i][6].ToString());
                aux.ID_Lot_Art = Int32.Parse(dt.Rows[i][7].ToString());

                aux.ID_Articulo =Int32.Parse(dt.Rows[i][8].ToString());;            
        
                aux.Cantidad = (float)(Convert.ToDouble(dt.Rows[i][9].ToString()));
                aux.costoU = (float)(Convert.ToDouble(dt.Rows[i][10].ToString()));
                aux.IVA = (float)(Convert.ToDouble(dt.Rows[i][11].ToString()));
                aux.ID_Moneda = Int32.Parse(dt.Rows[i][12].ToString()); 
                aux.Moneda = dt.Rows[i][13].ToString();
                aux.descuento = (float)(Convert.ToDouble(dt.Rows[i][14].ToString()));
                aux.TC = (float)(Convert.ToDouble(dt.Rows[i][15].ToString()));

                ListaRes.Add(aux);
            }

            return ListaRes;
        }

        public List<string> getLotesNames()
        { 
        
            string query = "";

            List<string> res = new List<string>();
            DataTable dt = new DataTable();

            query = "EXEC SP_GET_LOTES_NAMES";
            dt = SrvDB.ExecuteQuery(query);

            for (int i = 0; i < dt.Rows.Count; i++) {

                res.Add(dt.Rows[i][0].ToString());
            }

            return res;
        }

        public void insertITEM() {
            
            string query = "";
            query += "EXEC SP_ACTUALIZAR_FACTURA @ID_LOTE = " + ID_Lote;
            query += ", @FACTURA = '" + factura + "'";
            query += ", @IMPORTE = " + importe_f;

            SrvDB.ExecuteNonQuery(query);

            query = "";
            query += "EXEC SP_DELETE_OLD_ITEMS @ID_LOTE = " + ID_Lote;
            query += ", @ID_POM = " + ID_POM;
            query += ", @ID_PROV = " + ID_Proveedor;

            SrvDB.ExecuteNonQuery(query);

            query = "";

            for (int i = 0; i < ListaRecibida.Count; i++)
            {
                query += "EXEC SP_INSERT_ITEM @ITEM = " + ListaRecibida[i].ID_Item;
                query += ", @TRACK = " + ListaRecibida[i].ID_Track;
                query += ",	@NOMBRE_ART = '" + ListaRecibida[i].Nombre_Articulo + "'";
                query += ", @SERIE = '" + ListaRecibida[i].Serie + "'";
                query += ", @SKU = '" + ListaRecibida[i].Sku + "'";
                query += ", @IS_ARRENDADO = " + ListaRecibida[i].Is_Arrendado;
                query += ", @ID_LOTE = " + ID_Lote;                
                query += ", @COSTO_FINAL = " + ListaRecibida[i].Costo_Final;

              	query += ", @ID_ARTICULO = " + ListaRecibida[i].ID_Articulo;
	            query += ", @CANTIDAD = " + ListaRecibida[i].Cantidad;
	            query += ", @COSTOU = " + ListaRecibida[i].costoU;
	            query += ", @IVA = " + ListaRecibida[i].IVA;
                query += ", @ID_MONEDA = " + ListaRecibida[i].ID_Moneda;
                query += ", @ID_POM = " + ID_POM;
                query += ", @ID_PROV = " + ID_Proveedor;
                query += ", @TC = " + ListaRecibida[i].TC;
                query += ", @EMAIL = " + "'prueba@prueba.com.mx'";
                query += ", @DESCUENTO = " + ListaRecibida[i].descuento + ";";                            
            }

            if (!query.Equals("")) {
                SrvDB.ExecuteNonQuery(query);
            }
        }
    }
}

